﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Sunimage.Spjg.BLL.SysMgr;
using Sunimage.Spjg.Common.SysMgr;
using Sunimage.Spjg.Web.Helper;
using Sunimage.Spjg.Web.UserControls;
using Sunimage.Spjg.Common.Exceptions;
using Sunimage.Spjg.Common;
using System.Data.SqlClient;

namespace Sunimage.Spjg.Web.Admin
{
    public partial class UsersEdit : System.Web.UI.Page
    {
        UserBl userBl = new UserBl();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int id = Convert.ToInt32(Request["id"]);

                User user = userBl.GetById(id);
                tbxUsername.Text = user.Username;
                tbxEmail.Text = user.Email;
                tbxNickname.Text = user.NickName;
                ddlRole.SelectedValue = ((int)user.Role).ToString();
            }
        }

        protected void btnSaveEditUser_Click(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(Request["id"]);
            User user = userBl.GetById(id);
            user.Email = tbxEmail.Text.Trim();
            user.NickName = tbxNickname.Text.Trim();
            user.Role = (RoleType)Convert.ToInt32(ddlRole.SelectedValue);

            try
            {
                userBl.UpdateUser(user);
                MessageHelper.RegShowJSAjax(btnSaveEditUser, "修改用户成功", this.Page);
            }
            catch (ApplicationException excp)
            {
                //mpeEditUser.Show();
                AlertHelper.ShowError(excp.Message, this.Page);
            }
        }

        protected void btnCancelEditUser_Click(object sender, EventArgs e)
        {
            Response.Redirect("Users.aspx");
        }

    }
}